package cn.rongcloud.im.ui.moudle.convenient.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.rongcloud.im.R;
import cn.rongcloud.im.ui.moudle.convenient.adapter.ConvenientStoreChildlAdapter;
import cn.rongcloud.im.ui.moudle.convenient.adapter.ConvenientStoreGroupAdapter;
import cn.rongcloud.im.ui.moudle.convenient.bean.ConvenientStoreChildBean;
import cn.rongcloud.im.ui.moudle.convenient.bean.ConvenientStoreGroupBean;

/**
 * Created by lincheng on 2017/3/15.
 */

public class ConvenientStoreActivity extends Activity implements ConvenientStoreGroupAdapter.OnGroupClickListener {
    /**左边部分的组RecycleView*/
    private RecyclerView mGroupItemRecycleView;
    /**右边部分的子RecycleView*/
    private RecyclerView mChildItemRecycleView;
    /**包含组Bean的List*/
    private List<ConvenientStoreGroupBean> mGroupBeanList;
    /**包含字Bean的List*/
    private List<ConvenientStoreChildBean> mChildBeanList;
    /**左边组的适配器*/
    private ConvenientStoreGroupAdapter mGroupAdapter;
    /**右边子的适配器*/
    private ConvenientStoreChildlAdapter mChildAdapter;
    /**前一次点击的GroupPosition ，防止重复点击刷新UI*/
    private int mPreCickGroupPosition;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_convenient_store);
        initParms();
        initView();
        setView();
        setListener();
    }

    /**
     * 初始化参数
     */
    private void initParms() {
        mPreCickGroupPosition = 0;
        mGroupBeanList = new ArrayList<>();
        mChildBeanList = new ArrayList<>();
        makefuseData();
    }

    /**
     * 制作假的数据
     */
    private void makefuseData() {
        List<ConvenientStoreChildBean> childBeanList=null;
        for(int i= 0 ; i< 8 ;i++ ){
            ConvenientStoreGroupBean groupBean = new ConvenientStoreGroupBean();
            if(i == 0){
                groupBean.setGroupClicked(true);
            }
            groupBean.setGroupName("组别"+(i+1));
            childBeanList = new ArrayList<>();
            for(int j = 0 ; j < 5 ; j++ ){
                ConvenientStoreChildBean childBean = new ConvenientStoreChildBean();
                childBean.setStoreName("组别"+(i+1) + "--产品"+ (j+1));
                childBean.setMoneyCount(15+j);
                childBeanList.add(childBean);
            }
            groupBean.setChildBeanList(childBeanList);
            mGroupBeanList.add(groupBean);
        }
    }

    /**
     * 设置监听
     */
    private void setListener() {
        mGroupAdapter.setOnGroupClickListener(this);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mGroupItemRecycleView = (RecyclerView) findViewById(R.id.rv_group_item);
        mChildItemRecycleView = (RecyclerView) findViewById(R.id.rv_detail_item);
    }

    /**
     * 设置控件
     */
    private void setView() {
        setGroupRecycleView();
        setChildRecycleView();
    }

    /**
     * 设置子的RecycleView
     */
    private void setChildRecycleView() {
        mChildItemRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mChildBeanList.addAll( mGroupBeanList.get(0).getChildBeanList());
        mChildAdapter=new ConvenientStoreChildlAdapter(this ,mChildBeanList);
        mChildItemRecycleView.setAdapter(mChildAdapter);
    }

    /**
     * 设置组的RecycleView
     */
    private void setGroupRecycleView() {
        mGroupItemRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mGroupAdapter =new ConvenientStoreGroupAdapter(this ,mGroupBeanList);
        mGroupItemRecycleView.setAdapter(mGroupAdapter);
    }

    @Override
    public void onClick(int position) {
        if(mPreCickGroupPosition == position || null ==mGroupBeanList|| mGroupBeanList.size() == 0){
            return;
        }
        updateGroupTextState(position);
        updateChildList(position);
    }

    /**
     * 更新当前点击的组的状态
     * @param position    当前点击的组位置
     */
    private void updateGroupTextState(int position) {
        ConvenientStoreGroupBean groupBean =null;
        for(int i = 0 ; i < mGroupBeanList.size() ; i++){
             groupBean = mGroupBeanList.get(i);
            if(i == position){
                groupBean.setGroupClicked(true);
            }else{
                groupBean.setGroupClicked(false);
            }
        }
        mGroupAdapter.notifyDataSetChanged();
        mPreCickGroupPosition = position;
    }

    /**
     * 更新右边的子List数据
     * @param position    当前点击的组位置
     */
    private void updateChildList(int position) {
        mChildBeanList.clear();
        mChildBeanList.addAll(mGroupBeanList.get(position).getChildBeanList());
        mChildAdapter.notifyDataSetChanged();
    }
}
